package com.webbuilder.task;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.webbuilder.utils.DbUtil;
import com.webbuilder.utils.StringUtil;

public class ConfirmComplete implements Job {
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		System.out.println("开始进行定时确认完成");
		Connection conn = null;
		PreparedStatement sel_st = null;
		PreparedStatement selDays_st = null;
		PreparedStatement updateTotal_st = null;
		PreparedStatement updateDetail_st = null;
		ResultSet sel_rs = null;
		ResultSet selDays_rs = null;
			try {
				conn = DbUtil.getConnection();
				//更新购买总表
				String updateTotalSql=" update purchase_commodity_buy_total set order_status='04' where id= ? ";
				updateTotal_st=conn.prepareStatement(updateTotalSql);
				//更新购买明细表
				String updateDetailSql="update purchase_commodity_buy set order_status='04' where total_id= ? ";
				updateDetail_st=conn.prepareStatement(updateDetailSql);
				String daysSql="select * from wb_var where VAR_NAME='confirm_days'";
				selDays_st=conn.prepareStatement(daysSql);
				selDays_rs=selDays_st.executeQuery();
				if(selDays_rs.next()){
					if(!StringUtil.isEmpty(selDays_rs.getString("VAR_VALUE"))){
					int days=selDays_rs.getInt("VAR_VALUE");
					days=days*24*60*60;
					String commodityOrderSql="select * from purchase_commodity_buy_total where order_status='08' and UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(receipt_date) >= "+days+" ";
					sel_st=conn.prepareStatement(commodityOrderSql);
					sel_rs=sel_st.executeQuery();
					while(sel_rs.next()){
						updateTotal_st.setString(1, sel_rs.getString("id"));
						updateDetail_st.setString(1, sel_rs.getString("id"));
						updateTotal_st.addBatch();
						updateDetail_st.addBatch();
					  }
					updateDetail_st.executeBatch();
					updateTotal_st.executeBatch();
					}
				}
			} catch (Throwable e) {
				DbUtil.closeConnection(conn, true);
			} finally {
				DbUtil.closeStatement(updateDetail_st);
				DbUtil.closeStatement(updateTotal_st);
				DbUtil.closeStatement(sel_st);
				DbUtil.closeStatement(selDays_st);
				DbUtil.closeResultSet(sel_rs);
				DbUtil.closeResultSet(selDays_rs);
				DbUtil.closeConnection(conn, false);
			}
	}
}